using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using Sirenix.OdinInspector;
using ClassGenerator;
using UnityEngine;
using Pangoo.Common;



#if UNITY_EDITOR
using System.Reflection;
using UnityEditor;
#endif

namespace MetaTable
{

    [Serializable]
    public class MetaTableDataEntry
    {

        [ReadOnly]
        [VerticalGroup("基本信息")]
        [LabelText("配置")]
        public MetaTableConfig Config;

        [VerticalGroup("基本信息")]
        [LabelText("命名空间")]
        public string Namespace
        {
            get
            {
                return Config.Namespace;
            }
        }

        // [ReadOnly]
        [VerticalGroup("基本信息")]
        [LabelText("表名")]
        [TableColumnWidth(240, resizable: false)]
        public string ClassName;

        [VerticalGroup("基本信息")]
        [LabelText("表中文名")]
        public string ClassCnName;

        [VerticalGroup("基本信息")]
        [LabelText("添加的命名空间")]
        public string[] AddNamespaces;


        [VerticalGroup("内容编辑")]
        [LabelText("表头")]
        [TableList(AlwaysExpanded = true)]
        public List<MetaTableColumn> Columns = new List<MetaTableColumn>();


        public string BaseName => NameUtility.ToTitleCase(ClassName);

#if UNITY_EDITOR

        [Button("生成数据脚本")]
        [BoxGroup("基本信息/操作")]

        public void GeneratorDataCode()
        {
            if (Columns.Count == 0 || Config == null)
            {
                return;
            }


            var classBaseName = NameUtility.ToTitleCase(ClassName);
            var rowName = $"{classBaseName}";


            var classGenerateDir = Path.Join(Config.ScriptDataGenerateDir, classBaseName);
            DirectoryUtility.ExistsOrCreate(classGenerateDir);

            var rowJson = CodeGeneratorJson.BuildRowCodeJson(Columns);
            if (rowJson == null)
            {
                return;
            }

            GenerateData(classGenerateDir, rowName, rowJson, classBaseName);



            AssetDatabase.Refresh();
        }

        [Button("删除数据脚本")]
        [BoxGroup("基本信息/操作")]
        public void RemoveDataCode()
        {

            var classBaseName = NameUtility.ToTitleCase(ClassName);
            var classGenerateDir = Path.Join(Config.ScriptDataGenerateDir, classBaseName).ToPath();
            Debug.Log($"Data Generate Dir:{classGenerateDir}");
            if (Directory.Exists(classGenerateDir))
            {
                Debug.Log($"Remove Dir:{classGenerateDir}");
                AssetDatabase.DeleteAsset(classGenerateDir);
            }



            // Custom Code
            var classCustomDir = Path.Join(Config.ScriptDataCustomDir, classBaseName).ToPath();
            Debug.Log($"Data Custom Dir:{classCustomDir}");
            if (Directory.Exists(classCustomDir))
            {
                Debug.Log($"Remove Dir:{classCustomDir}");
                AssetDatabase.DeleteAsset(classCustomDir);

            }

            AssetDatabase.Refresh();
        }

        public void GenerateData(string classGenerateDir, string rowName, string rowJson, string baseName)
        {
            //Generator
            var codeRowPath = Path.Join(classGenerateDir, $"{rowName}.cs");
            List<string> headers = new List<string>();
            headers.AddRange(Config.UsingNamespace);
            headers.AddRange(AddNamespaces);
            JsonClassGenerator.GeneratorCodeString(rowJson, Namespace, new CSharpCodeDataWriter(headers, Columns), rowName, codeRowPath, baseClass: "MetaTableData");

            // Custom Code
            var classCustomDir = Path.Join(Config.ScriptDataCustomDir, baseName);
            DirectoryUtility.ExistsOrCreate(classCustomDir);
            var codeRowCustomPath = Path.Join(classCustomDir, $"{rowName}.Custom.cs");
            if (!File.Exists(codeRowCustomPath))
            {
                JsonClassGenerator.GeneratorCodeString("{}", Namespace, new CSharpCodeMetaTableBaseWriter(Config.UsingNamespace), rowName, codeRowCustomPath, isSerializable: false, isWriteFileHeader: false);
            }
        }

#endif

    }
}

